# File: components/Makefile
TOP_DIR := ..
include $(TOP_DIR)/buildcfg/modulescfg.mk
include $(TOP_DIR)/buildcfg/toolscfg.mk
include $(TOP_DIR)/buildcfg/includecfg.mk
include $(TOP_DIR)/buildcfg/sourcecfg.mk

# 编译器及参数设定
# ---------------------------------------------------------------------------
LIB_NAME            := libcomponents.a

SRC_FILES           := $(foreach mod,$(COMPONENTS_SRC_DIRS),$(wildcard $(mod)/*.c))
OBJ_FILES           := $(SRC_FILES:.c=.o)
DEP_FILES           := $(OBJ_FILES:.o=.d)
CLEAN_FILES         += $(LIB_NAME) $(OBJ_FILES) $(DEP_FILES)

CXXFLAGS            += -MMD -MP
LOCAL_DEF_FLAGS     += $(DEFFLAGSS) $(GITFLAGS) -DBIN=\"COMPONENTS\"
# 输出模式控制
# ---------------------------------------------------------------------------
ifneq ($(V),1)
quiet = quiet_
Q = @
else
quiet =
Q =
endif

# 命令定义 (格式: quiet_cmd_<类型> + cmd_<类型>)
# ---------------------------------------------------------------------------
quiet_cmd_cc    = CC      $<
      cmd_cc    = $(CXX) $(LOCAL_DEF_FLAGS) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

quiet_cmd_ld    = LD      $@
      cmd_ld    = $(CXX) $^ -o $@ $(LDFLAGS) $(LIBS)

quiet_cmd_ar    = AR      $@
      cmd_ar    = $(AR) rcs $@ $^

quiet_cmd_clean = CLEAN   $(CLEAN_FILES)
      cmd_clean = rm -f $(CLEAN_FILES)

# 通用命令执行函数 用法: $(call cmd,<命令类型>,<显示参数>)
# ---------------------------------------------------------------------------
cmd = $(Q)$(if $($(quiet)cmd_$(1)),\
    @printf "  %-8s%s\n" "$($(quiet)cmd_$(1))"; $(cmd_$(1)),\
    $(cmd_$(1)))

# 构建规则
# ---------------------------------------------------------------------------
all: $(LIB_NAME)

$(LIB_NAME): $(OBJ_FILES)
	$(call cmd,ar)

%.o: %.c
	$(call cmd,cc,$<)

# 包含自动生成的依赖
# ---------------------------------------------------------------------------
-include $(DEP_FILES)

# clean
# ---------------------------------------------------------------------------
clean:
	$(call cmd,clean)

.PHONY: all clean
